package com.hzgj.bcl.soa.rpc;

import com.hzgj.bcl.concurrent.FutureListener;
import com.hzgj.bcl.soa.util.NotifyingFuture;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class NullFuture<T> implements NotifyingFuture<T> {
    final T retval;

    public NullFuture(T retval) {
        this.retval = retval;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return true;
    }

    @Override
    public boolean isCancelled() {
        return true;
    }

    @Override
    public boolean isDone() {
        return true;
    }

    @Override
    public T get() throws InterruptedException, ExecutionException {
        return retval;
    }

    @Override
    public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return retval;
    }

    @Override
    public NotifyingFuture setListener(FutureListener<T> listener) {
        if (listener != null) {
            listener.futureDone(this);
        }
        return this;
    }
}
